package com.wang.head.controller;

import com.alibaba.fastjson.JSONObject;
import com.wang.common.controller.BaseController;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.PageInfoUtil;
import com.wang.orders.pojo.ACart;
import com.wang.orders.pojo.AOrders;
import com.wang.orders.service.IOrdersService;
import com.wang.users.pojo.AUsers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 存放的是订单模块
 */
@Controller
@RequestMapping("/head/orders")
public class OrdersHeadController extends BaseController
{
    @Resource
    private IOrdersService ordersService ;

    /**
     * 购物车列表
     * @return
     */
    @RequestMapping("/cartList")
    public String cartList(HttpSession session, Model model)
    {
        ConstatFinalUtil.SYS_LOGGER.info("==cartList==");
        /* 先获取当前用户 */
        AUsers users = (AUsers) session.getAttribute("users");

        /* 根据用户id查询cart */
        Map<String,Object> condMap = new HashMap<>();

        condMap.clear();
        condMap.put("usersId",users.getId());
        List<ACart> cartList = this.ordersService.selectCondListCartService(null, condMap);
        /* 把结果放到request中 */
        model.addAttribute("list",cartList);
        return "/head/orders/cartList" ;
    }

    /**
     * 购物车添加提交操作
     * @return
     */
    @RequestMapping("/cartInsertSubmit")
    public String cartInsertSubmit(String goodsId,HttpSession session)
    {
        ConstatFinalUtil.SYS_LOGGER.info("==cartInsertSubmit==");
        /* 先获取当前用户 */
        AUsers users = (AUsers) session.getAttribute("users");

        /* 添加购物车 */
        ACart cart = new ACart() ;
        cart.setUsersId(users.getId());
        cart.setGoodsId(Integer.valueOf(goodsId));
        cart.setNum(1);
        cart.setPubTime(new Date());
        JSONObject resultJSON = this.ordersService.saveOneCartService(cart);
        ConstatFinalUtil.SYS_LOGGER.info("==保存购物车结果:{}==",resultJSON.toJSONString());

        /* 客户端跳转到购物车列表页面 */
        return "redirect:/head/orders/cartList.mvc" ;
    }

    /**
     * 订单列表
     * @return
     */
    @RequestMapping("/ordersList")
    public String ordersList(HttpSession session, Model model)
    {
        ConstatFinalUtil.SYS_LOGGER.info("==ordersList==");
        /* 先获取当前用户 */
        AUsers users = (AUsers) session.getAttribute("users");

        /* 根据用户id查询cart */
        Map<String,Object> condMap = new HashMap<>();

        condMap.clear();
        condMap.put("usersId",users.getId());
        List<AOrders> ordersList = this.ordersService.selectCondListOrdersService(null, condMap);
        /* 把结果放到request中 */
        model.addAttribute("list",ordersList);
        return "/head/orders/ordersList" ;
    }

    /**
     * 订单添加提交操作(下单)
     * @return
     */
    @RequestMapping("/ordersInsert")
    public String ordersInsert(HttpSession session,Model model)
    {
        ConstatFinalUtil.SYS_LOGGER.info("==ordersInsert==");
        /* 先获取当前用户 */
        AUsers users = (AUsers) session.getAttribute("users");

        /* 根据用户id查询cart */
        Map<String,Object> condMap = new HashMap<>();

        condMap.clear();
        condMap.put("usersId",users.getId());
        List<ACart> cartList = this.ordersService.selectCondListCartService(null, condMap);
        /* 把结果放到request中 */
        model.addAttribute("list",cartList);

        /* 客户端跳转到购物车列表页面 */
        return "/head/orders/ordersInsert" ;
    }

    /**
     * 订单添加提交操作(下单)
     * @return
     */
    @RequestMapping("/ordersInsertSubmit")
    public String ordersInsertSubmit(HttpServletRequest request,HttpSession session)
    {
        ConstatFinalUtil.SYS_LOGGER.info("==ordersInsertSubmit==");
        /* 先获取当前用户 */
        AUsers users = (AUsers) session.getAttribute("users");
        /* 接收参数 */
        String receName = request.getParameter("receName");
        String recePhone = request.getParameter("recePhone");
        String receAddress = request.getParameter("receAddress");
        String content = request.getParameter("content");

        /* 创建对象 */
        AOrders orders = new AOrders();

        orders.setUsersId(users.getId());
        orders.setReceName(receName);
        orders.setRecePhone(recePhone);
        orders.setReceAddress(receAddress);

        /* 下单 */
        JSONObject resultJSON = this.ordersService.saveOneOrdersService(orders);
        ConstatFinalUtil.SYS_LOGGER.info("==结果:{}==",resultJSON.toJSONString());

        /* 客户端跳转到购物车列表页面 */
        return "redirect:/head/orders/ordersList.mvc" ;
    }
}
